//package com.zenchn.config.interceptor;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.zenchn.entity.HttpResult;
//import com.zenchn.redis.service.IpBlackListRedisService;
//import com.zenchn.utils.IPUtils;
//import org.springframework.http.HttpStatus;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.HandlerInterceptor;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.PrintWriter;
//
///**
// * IP黑名单拦截器
// * @author panbingqi
// * @date 2021-06-01
// */
//@Component
//public class IpBlacklistInterceptor implements HandlerInterceptor {
//
//    @Resource
//    private IpBlackListRedisService ipBlackListRedisService;
//
//    public static ObjectMapper objectMapper=new ObjectMapper();
//
//    /**
//     * 在请求处理之前进行调用
//     * @param request
//     * @param response
//     * @param handler
//     * @return
//     * @throws Exception
//     */
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
//            throws Exception {
//
//        boolean isRefuse=false;
//
//        String currIpAddress= IPUtils.getRealIP(request);
//        if (null!=currIpAddress && !currIpAddress.isEmpty()){
//            if (ipBlackListRedisService.hasExist(currIpAddress)){
//                isRefuse=true;
//            }
//        }else{
//            isRefuse=true;
//        }
//
//        if(isRefuse){
//            HttpResult result = new HttpResult(request.getRequestURI());
//            result.setMessage("HTTP 403.6 - 禁止访问：IP 地址被拒绝.");
//            result.setStatus(HttpStatus.FORBIDDEN.value());
//            result.setError(HttpStatus.FORBIDDEN.getReasonPhrase());
//            response.setStatus(HttpStatus.FORBIDDEN.value());
//            response.setContentType("application/json;charset=UTF-8");
//            response.setCharacterEncoding("UTF-8");
//
//            PrintWriter out = null;
//            try{
//                out = response.getWriter();
//                out.write(objectMapper.writeValueAsString(result));
//                out.flush();
//            }catch(IOException e){
//                e.printStackTrace();
//            }
//            return false;
//        }
//
//        return true;
//    }
//
//
//
//}
